const Router=require('koa-router');
const users=new Router(); 
const sqldata=require('../sqldata')
//用户登录
users.post('/',async(ctx)=>{
    let val=ctx.request.body
    let sql;
    let mydata1= await new Promise((resolve, reject)=>{
        sql=`select * from user where uid='${val.uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    if(mydata1.length>0){
        ctx.body=mydata1;
    }else{
        //首次登录
        let mydata2 = await new Promise((resolve, reject)=>{
            sql=`insert into user (uid,uname,uimage) values('${val.uid}','${val.uname}','${val.uimage}')`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        ctx.body=mydata2;
    }
})
//收藏
users.get('/my-collect',async(ctx)=>{
    let sql=`select * from collect,works,authorfoot where collect.uid='${ctx.request.query.uid}' and collect.wid=works.wid and works.aid=authorfoot.aid`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata;
})
//反馈获取
users.get('/my-feedback',async(ctx)=>{  
    let sql=`select * from user where uid='${ctx.request.query.uid}'`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata;
})
users.post('/my-feedback',async(ctx)=>{  
    let sql=`update user set ufeedback='${ctx.request.body.ufeedback}' where uid='${ctx.request.body.uid}'`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata;
})

module.exports=users;